home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / text / hyper / ADtoHT2_0.lha / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-19  |  8.9 KB  |  369 lines

  1. #ifndef EXEC_EXECBASE_H
  2. #include <exec/execbase.h>
  3. #endif
  4.  
  5. #ifndef DOS_DOSEXTENS_H
  6. #include <dos/dosextens.h>
  7. #endif
  8.  
  9. #include <proto/exec.h>
  10. #include <proto/dos.h>
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <errno.h>
  16.  
  17. /************************************************************************/
  18.  
  19. #include "main.h"
  20. #include "File.h"
  21. #include "Includes.h"
  22. #include "ProcessDir.h"
  23. #include "Autodocs.h"
  24. #include "AdditionalDocs.h"
  25.  
  26. /************************************************************************/
  27.  
  28. struct Arguments Arguments;
  29.  
  30. int Pass2;
  31.  
  32. BPTR Dirs[DIRCOUNT + 1];
  33.  
  34. #ifdef DEBUG
  35. FILE *DebugFile;
  36. #endif
  37.  
  38. /************************************************************************/
  39.  
  40. static int RC;
  41. static struct RDArgs *RDArgs;
  42.  
  43. /************************************************************************/
  44.  
  45. /* not static to avoid "not used" warning */
  46. char VersionString[] = "$VER: " PROGRAM_NAME " " PROGRAM_VERSION " (" PROGRAM_DATE ")" PROGRAM_COPYRIGHT "; compiled for " CPU " by " COMPILER;
  47.  
  48. /************************************************************************/
  49. /*                                                                      */
  50. /* Compare two AnyNodes                                                 */
  51. /*                                                                      */
  52. /************************************************************************/
  53.  
  54. int
  55. nodecmp (struct AnyNode *Node1, struct AnyNode *Node2)
  56.  
  57. {
  58.   int Result;
  59.  
  60.   Result = strcasecmp (Node1->Name, Node2->Name);
  61.   if (Result == 0)
  62.     {
  63.       Result = strcmp (Node1->Name, Node2->Name);
  64.     }
  65.   return Result;
  66. }
  67.  
  68. int
  69. nodecasecmp (struct AnyNode *Node1, struct AnyNode *Node2)
  70.  
  71. {
  72.   return strcasecmp (Node1->Name, Node2->Name);
  73. }
  74.  
  75. /************************************************************************/
  76. /*                                                                      */
  77. /* Search an AnyNode                                                    */
  78. /*                                                                      */
  79. /************************************************************************/
  80.  
  81. struct AnyNode *
  82. SearchNode (struct AVLTree *Tree, char *Name)
  83.  
  84. {
  85.   struct AnyNode AnyNode;
  86.  
  87.   AnyNode.Name = Name;
  88.   return (struct AnyNode *) AVL_SearchNode (Tree, &AnyNode.AVLNode);
  89. }
  90.  
  91. /************************************************************************/
  92. /*                                                                      */
  93. /* Create an AnyNode.                                                   */
  94. /*                                                                      */
  95. /************************************************************************/
  96.  
  97. struct AnyNode *
  98. CreateNode (const char *Name, size_t Size)
  99.  
  100. {
  101.   struct AnyNode *AnyNode;
  102.  
  103.   AnyNode = xmalloc (Size);
  104.   AnyNode->Name = xstrdup (Name);
  105.   return AnyNode;
  106. }
  107.  
  108. /************************************************************************/
  109. /*                                                                      */
  110. /* Set the return code for the shell                                    */
  111. /*                                                                      */
  112. /************************************************************************/
  113.  
  114. void
  115. SetRC (int NewRC)
  116.  
  117. {
  118.   if (RC < NewRC)
  119.     RC = NewRC;
  120. }
  121.  
  122. /************************************************************************/
  123. /*                                                                      */
  124. /* General cleanup.                                                     */
  125. /*                                                                      */
  126. /************************************************************************/
  127.  
  128. void
  129. CloseAll (int NewRC)
  130.  
  131. {
  132.   SetRC (NewRC);
  133.  
  134.   if (CallMatchEnd)
  135.     MatchEnd (&AnchorPath.AnchorPath);
  136.  
  137.   CurrentDir (Dirs[CURRENTDIR]);
  138.  
  139.   RClose ();
  140.   WClose ();
  141.  
  142.   FreeArgs (RDArgs);
  143.  
  144.   {
  145.     int i;
  146.     for (i = 0; i < DIRCOUNT; i++)
  147.       {
  148.     if (Dirs[i])
  149.       UnLock (Dirs[i]);
  150.       }
  151.   }
  152.  
  153.   exit (RC);
  154. }
  155.  
  156. /************************************************************************/
  157. /*                                                                      */
  158. /* xmalloc(): allocate a block, exit on error                           */
  159. /*                                                                      */
  160. /************************************************************************/
  161.  
  162. void *xmalloc (size_t Size)
  163.  
  164. {
  165.   int FirstError;
  166.  
  167.   FirstError=TRUE;
  168.   do
  169.     {
  170.       size_t *Memory;
  171.  
  172.       if ((Memory = malloc (Size)))
  173.     {
  174.       if (!FirstError)
  175.         {
  176.           fprintf(stderr,"Allocation succeeded.\n");
  177.         }
  178.       return Memory;
  179.     }
  180.       if (FirstError)
  181.     {
  182.       perror (PROGRAM_NAME);
  183.       FirstError=FALSE;
  184.     }
  185.       Delay(2*TICKS_PER_SECOND);
  186.     }
  187.   while (!CheckSignal(SIGBREAKF_CTRL_C));
  188.   errno=ERROR_BREAK;
  189.   perror(NULL);
  190.   CloseAll (RETURN_ERROR);
  191. }
  192.  
  193. /************************************************************************/
  194. /*                                                                      */
  195. /* xrealloc(): re-allocate a block, exit on error.            */
  196. /*                                                                      */
  197. /************************************************************************/
  198.  
  199. void *xrealloc(void *Memory, size_t Size)
  200.  
  201. {
  202.   int FirstError;
  203.  
  204.   FirstError=TRUE;
  205.   do
  206.     {
  207.       size_t *NewMemory;
  208.  
  209.       if ((NewMemory=realloc(Memory,Size)))
  210.     {
  211.       if (!FirstError)
  212.         {
  213.           fprintf(stderr,"Allocation succeeded.\n");
  214.         }
  215.       return NewMemory;
  216.     }
  217.       if (FirstError)
  218.     {
  219.       perror(PROGRAM_NAME);
  220.       FirstError=FALSE;
  221.     }
  222.     }
  223.   while (!CheckSignal(SIGBREAKF_CTRL_C));
  224.   errno=ERROR_BREAK;
  225.   perror(NULL);
  226.   CloseAll(RETURN_ERROR);
  227. }
  228.  
  229. /************************************************************************/
  230. /*                                                                      */
  231. /* Duplicate a string, exit on error                                    */
  232. /*                                                                      */
  233. /************************************************************************/
  234.  
  235. char *xstrdup (const char *String)
  236.  
  237. {
  238.   char *New;
  239.  
  240.   New = xmalloc (strlen (String) + 1);
  241.   strcpy (New, String);
  242.   return New;
  243. }
  244.  
  245. /************************************************************************/
  246. /*                                                                      */
  247. /* Init everything we need to get going.                                */
  248. /*                                                                      */
  249. /************************************************************************/
  250.  
  251. static void
  252. Init (void)
  253.  
  254. {
  255.   CurrentDir (Dirs[CURRENTDIR] = CurrentDir (NULL));
  256. }
  257.  
  258. /************************************************************************/
  259. /*                                                                      */
  260. /* Process the command line                                             */
  261. /*                                                                      */
  262. /************************************************************************/
  263.  
  264. static void
  265. GetParams (void)
  266.  
  267. {
  268.   static long DefaultWidth = 78;
  269.   int i;
  270.  
  271.   Arguments.Width = &DefaultWidth;
  272.  
  273.   if (!(RDArgs = ReadArgs ("INC/A,HINC/A,DOC/A,HDOC/A,"
  274.                "MASTER,"
  275.                "XREF,"
  276.                "FULLPATH/S,"
  277.                "VERSION/N,"
  278.                "PARENTHESES/S,"
  279.                "WIDTH/N"
  280. #ifdef DEBUG
  281.                ",DEBUG"
  282. #endif
  283.                ,(long *) &Arguments, NULL)))
  284.     {
  285.       errno=IoErr();
  286.       perror (PROGRAM_NAME);
  287.       CloseAll (RETURN_FAIL);
  288.     }
  289.  
  290.   if (*Arguments.Width < 40)
  291.     {
  292.       fprintf (stderr, "Warning: WIDTH must be at least 40. Using WIDTH=40.\n");
  293.       DefaultWidth = 40;
  294.       Arguments.Width = &DefaultWidth;
  295.       SetRC (RETURN_WARN);
  296.     }
  297.  
  298.   if (!Arguments.Version)
  299.     {
  300.       static long AmigaguideVersion;
  301.  
  302.       struct Library *Library;
  303.  
  304.       if ((Library = OpenLibrary ("amigaguide.library", 0)) ||
  305.       (Library = OpenLibrary ("version.library", 0)))
  306.     {
  307.       AmigaguideVersion = Library->lib_Version;
  308.       CloseLibrary (Library);
  309.     }
  310.       else
  311.     {
  312.       AmigaguideVersion = SysBase->LibNode.lib_Version;
  313.     }
  314.       Arguments.Version = &AmigaguideVersion;
  315.     }
  316.  
  317.   for (i = 0; i < DIRCOUNT; i++)
  318.     {
  319.       if (!(Dirs[i] = Lock (Arguments.Dirs[i], SHARED_LOCK)))
  320.     {
  321.       errno=IoErr();
  322.       perror (Arguments.Dirs[i]);
  323.       CloseAll (RETURN_FAIL);
  324.     }
  325.     }
  326.  
  327. #ifdef DEBUG
  328.   if (Arguments.Debug)
  329.     {
  330.       if (!(DebugFile=fopen(Arguments.Debug,"w")))
  331.     {
  332.       perror(Arguments.Debug);
  333.       CloseAll(RETURN_FAIL);
  334.     }
  335.       SetVBuf (fileno(DebugFile), NULL, BUF_FULL, 32 * 1024);
  336.     }
  337. #endif
  338. }
  339.  
  340. /************************************************************************/
  341.  
  342. void
  343. AmigaMain (void)
  344.  
  345. {
  346.   if (WorkbenchMessage)
  347.     {
  348.       exit (100);
  349.     }
  350.  
  351.   Init ();
  352.   GetParams ();
  353.  
  354.   puts ("*** First pass ***");
  355.   ProcessDir1 (INCLUDEDIR, "#?.h", ProcessIncludeFile1);
  356.   ProcessDir1 (AUTODOCDIR, "#?.doc", ProcessAutodocFile1);
  357.  
  358.   Pass2 = TRUE;
  359.  
  360.   WriteGlobalTOC ();
  361.   WriteXRef ();
  362.  
  363.   puts ("\n*** Second pass ***");
  364.   ProcessDir2 (INCLUDEDIR, &IncludeFileTree, (void (*)(struct AnyNode *)) ProcessIncludeFile2);
  365.   ProcessDir2 (AUTODOCDIR, &AutodocFileTree, (void (*)(struct AnyNode *)) ProcessAutodocFile2);
  366.  
  367.   CloseAll (RETURN_OK);
  368. }
  369.